package com.example.forms;

import java.util.ArrayList;

import javax.microedition.khronos.egl.EGL;

import org.json.JSONArray;
import org.json.JSONObject;

import com.example.sale24h.MainActivity;
import com.example.sale24h.R;
import com.example.sale24h.R.id;
import com.example.serverconnect.ITaskListener;
import com.sale24h.data.AppData;
import com.sale24h.data.clsChiTietBan;
import com.sale24h.data.clsMatHang;
import com.sale24h.data.clsXuatBan;
import com.sale24h.data.eFRMs;
import com.sale24h.data.adapter.ChonMonAdapter;
import com.sale24h.data.adapter.MathangAdapter;
import com.sale24h.tasks.GetXuatbanChiTietTask;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;

import android.os.Bundle;
import android.util.Log;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author Administrator
 *
 */
public class frmPhieuGoiMon extends android.support.v4.app.Fragment implements OnClickListener
{
	private ListView lstvMatHangs;
	private TextView tbxTenBan;
	
	private TextView tbxsokhach;
	
	private TextView lbsobuild;
	Button btngoimon,btnorder,btnthanhtoan;
	ChonMonAdapter lstMon;
	private Context context;
	
	private clsXuatBan xuatbanobj;
	
private static frmPhieuGoiMon instance;
	
	public static frmPhieuGoiMon getInstance()
	{
		if(instance==null)
		{
			instance=new frmPhieuGoiMon();
		}
		return instance;
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
		View rootView=inflater.inflate(R.layout.frmphieugoimon_layout, container,false);
		this.context=rootView.getContext();
		
		lstvMatHangs=(ListView)rootView.findViewById(R.id.lstvMatHangs);
		
		tbxTenBan=(TextView)rootView.findViewById(id.tbxTenBan);
		
		tbxTenBan.setText(AppData.getInstance().getCurrentBan().getTenban());
		
		tbxsokhach=(TextView)rootView.findViewById(id.tbxsonguoi);
		
		lbsobuild=(TextView)rootView.findViewById(id.lbNgayLapBuild);
		
		btngoimon=(Button) rootView.findViewById(R.id.btngoimon);
		btnorder=(Button) rootView.findViewById(R.id.btnOrder);
		btnthanhtoan=(Button) rootView.findViewById(R.id.btnThanhToan);
		btngoimon.setOnClickListener(this);
		btnorder.setOnClickListener(this);
		btnthanhtoan.setOnClickListener(this);
		show("test");
		
		lstvMatHangs.setOnItemSelectedListener(new OnItemSelectedListener()
		{

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id)
			{
				
				if(xuatbanobj!=null)
				{
										
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0)
			{
				
				
			}
		});
		
		LoadData();
		
		return rootView;
	}
	
	
	@Override
	public void onAttach(Activity activity)
	{
		// TODO Auto-generated method stub
		show("tro lai");
	}
	private void LoadData()
	{		
		//load exist data for this table.
		
		new GetXuatbanChiTietTask(new ITaskListener()
		{
			
			@Override
			public void PreRun()
			{
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void Completed(JSONObject result)
			{
				try
				{
					if(result!=null && result.getInt("success")==1)
					{
						//init xuatban object
						xuatbanobj=new clsXuatBan();
						//apply JSON object to real object
						JSONArray xuatbans=result.getJSONArray("xuatbans");
					if (xuatbans.length()>0)
					{
						JSONObject jobj=xuatbans.getJSONObject(0);
						xuatbanobj.ApplyfromJSON(jobj);
						//display information in form
					 lstMon=new ChonMonAdapter(context, android.R.layout.simple_list_item_1, xuatbanobj.getChitietbans());
						
						
						tbxsokhach.setText(String.valueOf(xuatbanobj.getSokhach()));
						lbsobuild.setText("Số: SMART-"+xuatbanobj.getMact());
					}else {
						lstMon=new ChonMonAdapter(context, android.R.layout.simple_list_item_1, new ArrayList<clsChiTietBan>());
					}
					
					lstvMatHangs.setAdapter(lstMon);
						
					}
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
				
			}
		}, this.context).execute();
	}
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
	{
		inflater.inflate(R.menu.goimon_menu, menu);
		super.onCreateOptionsMenu(menu, inflater);
	}
	@Override
	public void onClick(View v)
	{
		
		// TODO Auto-generated method stub
		
		switch (v.getId())
		{
		case R.id.btngoimon:
			
		{
			MainActivity.getInstance().gotoView(eFRMs.FRM_CHONMON);
		}break;
		case R.id.btnOrder :
		{
			
		}break;
		case R.id.btnThanhToan :
		{
			
		}break;
			
			

		
		}
	}
	
	/**@author vanhung
	 * @param matHang gia tri cua cua mat hang
	 */
	public void AddMonAn(clsMatHang matHang)
	{
		clsChiTietBan chiTietBan=getFromMatHang(matHang);
		if (lstMon==null)
		{
			lstMon=new ChonMonAdapter(context, android.R.layout.simple_list_item_1, new ArrayList<clsChiTietBan>());
			lstvMatHangs.setAdapter(lstMon);
			
		}
		
		lstMon.add(chiTietBan);
		lstMon.notifyDataSetChanged();
	}
	private clsChiTietBan getFromMatHang(clsMatHang mathang)
	{
		clsChiTietBan chiTietBan=new clsChiTietBan();
		chiTietBan.setChietkhau(mathang.getPhantram());
		chiTietBan.setDongia1(mathang.getDonGia1());
		chiTietBan.setDongia2(mathang.getDonGia2());
		chiTietBan.setDongia3(mathang.getDonGia3());
		chiTietBan.setMahang(mathang.getMaHang());
		chiTietBan.setTrangthai(mathang.getHetmon()==true?1:0);
		chiTietBan.setTenhang(mathang.getTenHang());
		chiTietBan.setTendonvi(mathang.getDonVi());
		return chiTietBan;
		
	}
	void show(String message)
	{
		Log.e("loi", message);
	}

}
